<template>
  <md-nav-bar title="选择我的音乐" backgroundColor="#ffffff" />
  <view class="container">
    <!-- 音乐卡片列表 -->
    <view class="music-list">
      <md-page-list :api="api.Music.myList" :params="{ isShowMusic: true }" @change="pageChange">
        <view
          :class="['music-card', { active: item.id === data?.currentSelect }]"
          @click="() => handleSelect(item.id)"
          v-for="(item, idx) in data.list"
          :key="idx">
          <image class="music-img" :src="item.coverUrl" mode="aspectFill"></image>
          <view class="music-info">
            <text class="music-title">{{ item.name }}</text>
            <text class="music-time">{{ item.createTime }}</text>
            <view class="music-meta">
              <view class="meta-item">
                <image class="meta-icon" src="https://cdn.xmxiaomai.com/isong_static/square/%E8%B5%9E.png"></image>
                <text>{{ item.likesNum }}</text>
              </view>
              <view class="meta-item">
                <image
                  class="meta-icon"
                  src="https://cdn.xmxiaomai.com/isong_static/square/%E8%AF%84%E8%AE%BA.png"></image>
                <text>{{ item.commentNum }}</text>
              </view>
              <view class="meta-item">
                <image class="meta-icon" src="https://cdn.xmxiaomai.com/isong_static/square/%E8%BD%AC.png"></image>
                <text>{{ item.collectNum }}</text>
              </view>
            </view>
          </view>
          <view class="music-score">
            <text class="score">{{ '80' }}</text>
            <text class="score-unit">分</text>
          </view>
        </view>
      </md-page-list>
    </view>

    <!-- 添加音乐按钮 -->
    <view class="add-btn-wrap">
      <button class="add-btn" @click="confirmMusic">确认选择音乐</button>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { reactive } from 'vue';
// 接口
import api from '@/api/index';
import type { Music } from '@/api/data';

const data = reactive({
  list: [] as Music.List.Data['list'],
  currentSelect: null as any,
});

const handleSelect = (id: number) => {
  data.currentSelect = id;
};

const confirmMusic = () => {
  // console.log(data.currentSelect);
  uni.$emit('select_myMusic', { musicId: data.currentSelect });
  uni.navigateBack();
};

const pageChange = (list: Music.List.Data['list']) => {
  data.list = list;
};
</script>

<style lang="scss" scoped>
.container {
  min-height: 100vh;
  background: #f6fcfa;
  padding-bottom: 120rpx;
}
.music-list {
  padding: 24rpx;
  box-sizing: border-box;
}
.music-card {
  display: flex;
  align-items: center;
  background: #eaf8f4;
  border-radius: 40rpx;
  box-shadow: 0 4rpx 16rpx 0 #e0e0e0;
  margin-bottom: 32rpx;
  padding: 24rpx 32rpx;
  position: relative;
  box-sizing: border-box;
  border: 1rpx solid #eaf8f4;
  &.active {
    border: 1rpx solid #1fa37a;
  }
}
.music-img {
  width: 120rpx;
  height: 120rpx;
  border-radius: 20rpx;
  margin-right: 24rpx;
  flex-shrink: 0;
}
.music-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.music-title {
  font-size: 32rpx;
  color: #222;
  font-weight: 600;
  margin-bottom: 8rpx;
  width: 320rpx;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.music-time {
  font-size: 24rpx;
  color: #888;
  margin-bottom: 12rpx;
}
.music-meta {
  display: flex;
  align-items: center;
}
.meta-item {
  display: flex;
  align-items: center;
  margin-right: 32rpx;
}
.meta-icon {
  width: 28rpx;
  height: 28rpx;
  margin-right: 6rpx;
}
.music-score {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-left: 24rpx;
}
.score {
  font-size: 56rpx;
  color: #1fa37a;
  font-weight: bold;
  line-height: 1;
}
.score-unit {
  font-size: 24rpx;
  color: #1fa37a;
  margin-top: -8rpx;
}
.add-btn-wrap {
  position: fixed;
  width: 100%;
  left: 0;
  bottom: 0;
  padding-bottom: $safe-bottom;
  margin: 60rpx 0 0 0;
  display: flex;
  justify-content: center;
  box-sizing: border-box;
}
.add-btn {
  width: 80vw;
  height: 90rpx;
  background: #1fa37a;
  color: #fff;
  font-size: 36rpx;
  border-radius: 60rpx;
  font-weight: 600;
  box-shadow: 0 4rpx 16rpx 0 #e0e0e0;
  border: none;
}
.tabbar {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100vw;
  height: 110rpx;
  background: #184c3a;
  display: flex;
  justify-content: space-around;
  align-items: center;
  z-index: 99;
  box-shadow: 0 -2rpx 12rpx 0 #e0e0e0;
}
.tabbar-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
}
.tabbar-icon {
  width: 48rpx;
  height: 48rpx;
  margin-bottom: 4rpx;
  opacity: 0.7;
}
.tabbar-icon.active {
  width: 80rpx;
  height: 80rpx;
  margin-bottom: 0;
  margin-top: -32rpx;
  background: #f6fcfa;
  border-radius: 50%;
  box-shadow: 0 4rpx 16rpx 0 #e0e0e0;
  opacity: 1;
}
.tabbar-text {
  font-size: 22rpx;
  color: #f6fcfa;
  opacity: 0.7;
}
.tabbar-text.active {
  color: #1fa37a;
  font-size: 28rpx;
  font-weight: bold;
  opacity: 1;
}
</style>
